package com.example.yichuangyun.mvvm.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;

import com.example.yichuangyun.R;
import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.ActivityMyCollectBinding;
import com.example.yichuangyun.dialog.ContactsCompanyDialog;
import com.example.yichuangyun.model.EventBusModel;
import com.example.yichuangyun.model.company.CompanyDataModel;
import com.example.yichuangyun.model.company.CompanyDetailsContactsDataModel;
import com.example.yichuangyun.mvvm.base.BaseActivity;
import com.example.yichuangyun.mvvm.ui.activity.view.MyCollectViewModel;
import com.example.yichuangyun.mvvm.ui.adapter.SearchCompanyAdapter;
import com.example.yichuangyun.utils.CommonUtil;
import com.example.yichuangyun.utils.JumpIntent;
import com.example.yichuangyun.utils.ToastHelper;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的收藏
 */
public class MyCollectActivity extends BaseActivity<ActivityMyCollectBinding, MyCollectViewModel> {

    private int page = 1;
    private SearchCompanyAdapter adapter;

    @Override
    protected MyCollectViewModel bindModel() {
        return getViewModel(this, MyCollectViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void initData() {
        initTitleBar(getString(R.string.my_collect));
        binding.setCollectViewModel(viewModel);
        adapter = new SearchCompanyAdapter(context, 1);
        binding.recyclerView.setAdapter(adapter);
        adapter.setEmptyViewEnable(true);
        adapter.setEmptyViewLayout(this, R.layout.loading_view);
//        adapter.setAnimationEnable(true);
//        adapter.setItemAnimation(BaseQuickAdapter.AnimationType.SlideInBottom);
        getMyCollectList(page);
    }

    private void getMyCollectList(int page) {
        Map<String, Object> map = new HashMap<>();
        map.put("keyWords", viewModel.keywords.getValue());
        map.put("page", page);
        viewModel.getMyCollectList(map);
    }

    @Override
    protected void initClick() {
        viewModel.collectDataModelMutableLiveData.observe(this, collectDataModel -> {
            adapter.setKeywords(viewModel.keywords.getValue());
            List<CompanyDataModel.CompanyModel> list = collectDataModel.getRecords();
            if (list.size() < Constant.pageSize) {
                binding.smartRefreshLayout.finishLoadMoreWithNoMoreData();
            }
            if (page == 1) {
                if (list.isEmpty()) {
                    adapter.setEmptyViewLayout(this, R.layout.layout_empty_list);
                }
                binding.smartRefreshLayout.resetNoMoreData();
                adapter.submitList(list);
                binding.smartRefreshLayout.finishRefresh();
            } else {
                adapter.addAll(list);
                binding.smartRefreshLayout.finishLoadMore();
            }
        });

        adapter.addOnItemChildClickListener(R.id.ll_like, (baseQuickAdapter, view, i) -> {
            CompanyDataModel.CompanyModel companyModel = baseQuickAdapter.getItem(i);
            if (null != companyModel) {
                viewModel.delCollectCompany(companyModel.getId(), companyModel.getEid());
                page = 1;
                getMyCollectList(page);
                ToastHelper.show(getString(R.string.cancel_collect_success));
            }
        });

        adapter.addOnItemChildClickListener(R.id.ll_call, (baseQuickAdapter, view, i) -> {
            CompanyDataModel.CompanyModel companyModel = baseQuickAdapter.getItem(i);
            if (null != companyModel) {
                viewModel.getContacts(companyModel.getEid());
            }
        });

        adapter.setOnItemClickListener((baseQuickAdapter, view, i) -> {
            CompanyDataModel.CompanyModel companyModel = baseQuickAdapter.getItem(i);
            if (null != companyModel) {
                Bundle bundle = new Bundle();
                bundle.putString(Constant.eidKey, companyModel.getEid());
                JumpIntent.jump(MyCollectActivity.this, CompanyDetailsActivity.class, bundle);
            }
        });

        adapter.addOnItemChildClickListener(R.id.tv_reg_address, (baseQuickAdapter, view, i) -> {
            CompanyDataModel.CompanyModel companyModel = baseQuickAdapter.getItem(i);
            if (null != companyModel) {
                Bundle bundle = new Bundle();
                bundle.putString(Constant.eidKey, companyModel.getEid());
                JumpIntent.jump(MyCollectActivity.this, CompanyDetailsActivity.class, bundle);
            }
        });

        adapter.addOnItemChildClickListener(R.id.tv_report_address, (baseQuickAdapter, view, i) -> {
            CompanyDataModel.CompanyModel companyModel = baseQuickAdapter.getItem(i);
            if (null != companyModel) {
                Bundle bundle = new Bundle();
                bundle.putString(Constant.eidKey, companyModel.getEid());
                JumpIntent.jump(MyCollectActivity.this, CompanyDetailsActivity.class, bundle);
            }
        });

        // 可拨打电话返回结果
        viewModel.contactsMutableLiveData.observe(this, contactsModels -> {
            if (null != contactsModels && !contactsModels.isEmpty()) {
                initDialog(contactsModels);
            } else {
                ToastHelper.show(getString(R.string.no_contact_phone));
            }
        });

        binding.smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getMyCollectList(page);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getMyCollectList(page);
            }
        });

        // 搜索
        viewModel.onDelayClick(binding.tvSearch, o -> {
            if (!TextUtils.isEmpty(binding.etSearchContent.getText().toString().trim())){
                CommonUtil.closeKeyboard(this);
                page = 1;
                binding.recyclerView.scrollToPosition(0);
                binding.smartRefreshLayout.autoRefresh();
            } else {
                ToastHelper.show(getString(R.string.please_input_search_company_content));
            }
        });

        // 键盘搜索
        binding.etSearchContent.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                if (!TextUtils.isEmpty(binding.etSearchContent.getText().toString().trim())){
                    CommonUtil.closeKeyboard(this);
                    page = 1;
                    binding.recyclerView.scrollToPosition(0);
                    binding.smartRefreshLayout.autoRefresh();
                } else {
                    ToastHelper.show(getString(R.string.please_input_search_company_content));
                }
                return true;
            }
            return false;
        });

        // 搜索输入框
        binding.etSearchContent.setOnTextChangeListener(text -> binding.ivClear.setVisibility(TextUtils.isEmpty(text.toString()) ? View.GONE : View.VISIBLE));

        // 搜索清除
        viewModel.onDelayClick(binding.ivClear, o -> {
            CommonUtil.closeKeyboard(this);
            binding.etSearchContent.setText("");
            binding.ivClear.setVisibility(View.GONE);
            page = 1;
            binding.recyclerView.scrollToPosition(0);
            binding.smartRefreshLayout.autoRefresh();
        });
    }

    private void initDialog(List<CompanyDetailsContactsDataModel.ContactsModel> contactsData) {
        ContactsCompanyDialog contactsCompanyDialog = new ContactsCompanyDialog(context, contactsData, 4);
        contactsCompanyDialog.showDialog();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(EventBusModel eventBusModel) {
        if (null != eventBusModel) {
            if (eventBusModel.getMsg().equals(Constant.refreshCollectList)) {
                page = 1;
                getMyCollectList(page);
            }
        }
    }
}